package com.learn.databinding.bindingadapters

import android.content.Context
import android.util.AttributeSet
import android.widget.ImageView
import android.widget.Toast
import androidx.databinding.BindingMethod
import androidx.databinding.BindingMethods

@BindingMethods(
    value = [
        //把属性"app:test"和方法showTestToast进行绑定
        //在调用"app:test"属性时，实际会调用showTestToast方法
        BindingMethod(
            type = android.widget.ImageView::class,
            attribute = "app:test",
            method = "showTestToast"
        )
    ]
)
class MyImageView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : ImageView(context, attrs, defStyleAttr) {

    /**
     * 该方法通过@BindingMethod注解和属性"app:test"绑定，
     * 在使用属性"app:test"时，就会调用该方法
     */
    fun showTestToast(str:String){
        Toast.makeText(context,"$str",Toast.LENGTH_SHORT).show()
    }
}